//
//  WDWorderOrderReviewVehDetailViewController.m
//  wd-serviceapp
//
//  Created by Bin Tang on 2024/8/31.
//

#import "WDWorderOrderReviewVehDetailViewController.h"
#import "WDWorderOrderReviewVehDetailViewModel.h"
#import "WDAppointTimeView.h" // 预约时间
#import "WDTaskTypeServFlowView.h" // 类型状态
#import "WDTaskDetailSectionView.h"
#import "WDWorderOrdRevVehDetailBasicInfoView.h" // 基本信息
#import "WDWorderOrdRevVehDetailCarInfoView.h" // 车辆信息
#import "WDWorderOrdRevVehDetailServiceInfoView.h" // 服务流程

@interface WDWorderOrderReviewVehDetailViewController ()

@property (nonatomic, strong) WDWorderOrderReviewVehDetailViewModel *viewModel;

/// 预约时间
@property (nonatomic, strong) WDAppointTimeView *appointTimeView;

/// 类型状态
@property (nonatomic, strong) WDTaskTypeServFlowView *typeFlowView;

/// 基本信息
@property (nonatomic, strong) WDTaskDetailSectionView *basicSectionView;

/// 基本信息
@property (nonatomic, strong) WDWorderOrdRevVehDetailBasicInfoView *basicInfoView;

/// 车辆信息
@property (nonatomic, strong) WDTaskDetailSectionView *carSectionView;

/// 车辆信息
@property (nonatomic, strong) WDWorderOrdRevVehDetailCarInfoView *carInfoView;

/// 服务流程
@property (nonatomic, strong) WDTaskDetailSectionView *serviceSectionView;

/// 服务流程
@property (nonatomic, strong) WDWorderOrdRevVehDetailServiceInfoView *serviceInfoView;

@end

@implementation WDWorderOrderReviewVehDetailViewController

- (void)dealloc {
    debugMethod();
    
}

- (void)initWithVar {
    [super initWithVar];
    
}

- (void)initWithViewModel {
    _viewModel = [WDWorderOrderReviewVehDetailViewModel new];
    _viewModel.serviceModel = self.serviceModel;
    [_viewModel setupData];
}

- (void)initWithNavBar {
    [super initWithNavBar];
    
    [self initWithBackButton];
    
    self.navigationItem.title = self.viewModel.title;
}

- (void)initWithSubView {
    [super initWithSubView];
    
    [self initWithRootContent:NO];
    
    [self fromServerRequestData];
}

- (void)updateWithUI {
    // 预约时间
    [self initWithAppointTimeView];
    
    // 类型状态
    [self initWithTypeFlowView];
    
    [self initWithBasicSectionView];
    
    // 基本信息
    [self initWithBasicInfoView];
    
    // 车辆信息
    [self initWithCarSectionView];
    
    // 车辆信息
    [self initWithCarInfoView];
    
    /// 服务流程
    [self initWithServiceSectionView];
    
    /// 服务流程
    [self initWithServiceInfoView];
    
    self.rootContent.contentSize = CGSizeMake(kScreenW, self.serviceInfoView.bottom + 10);
}

/// 预约时间
- (void)initWithAppointTimeView {
    if (!_appointTimeView) {
        CGRect frame = CGRectMake(0, 0, kScreenW, kWDAppointTimeViewHeight);
        _appointTimeView = [[WDAppointTimeView alloc] initWithFrame:frame];
        [self.rootContent addSubview:_appointTimeView];
    }
    if (self.viewModel.taskDetailModel) {
        [self.appointTimeView updateWithViewData:self.viewModel.taskDetailModel];
    }
}

/// 类型状态
- (void)initWithTypeFlowView {
    if (!_typeFlowView) {
        CGRect frame = CGRectMake(0, self.appointTimeView.bottom, kScreenW, kWDTaskTypeServFlowViewHeight);
        _typeFlowView = [[WDTaskTypeServFlowView alloc] initWithFrame:frame];
        [self.rootContent addSubview:_typeFlowView];
    }
    if (self.viewModel.taskDetailModel) {
        [self.typeFlowView updateWithViewData:self.viewModel.taskDetailModel];
    }
}

- (void)initWithBasicSectionView {
    if (!_basicSectionView) {
        CGRect frame = CGRectMake(0, self.typeFlowView.bottom, kScreenW, kWDTaskDetailSectionViewHeight);
        _basicSectionView = [[WDTaskDetailSectionView alloc] initWithFrame:frame];
        [self.rootContent addSubview:_basicSectionView];
        [self.basicSectionView updateWithViewData:@"基本信息"];
    }
}

/// 基本信息
- (void)initWithBasicInfoView {
    if (!_basicInfoView) {
        
        int rows = ceilf(self.viewModel.taskDetailModel.problemPhotoList.count / 3.0);
        float imgViewHeight = 0;
        if (self.viewModel.taskDetailModel.problemPhotoList.count > 0) {
            imgViewHeight = rows * 80 + 5 * (rows - 1) - 80;
        }
        
        float height = kWDTaskDetailBasicSingleViewHeight * 12 + (kWDTaskDetailBasicSingleViewHeight - 18 + self.viewModel.taskDetailModel.vehicleAdsHeight) + 14 + kWDTaskDetailBasicSingleImageViewHeight + imgViewHeight + 14;
        if (self.viewModel.taskDetailModel && self.viewModel.taskDetailModel.detailHeight > 10) {
            height += (self.viewModel.taskDetailModel.detailHeight - 18);
        }
        
        CGRect frame = CGRectMake(0, self.basicSectionView.bottom, kScreenW, height);
        _basicInfoView = [[WDWorderOrdRevVehDetailBasicInfoView alloc] initWithFrame:frame];
        [self.rootContent addSubview:_basicInfoView];
    }
    if (self.viewModel.taskDetailModel) {
        [self.basicInfoView updateWithViewData:self.viewModel.taskDetailModel];
    }
}

/// 车辆信息
- (void)initWithCarSectionView {
    if (!_carSectionView) {
        CGRect frame = CGRectMake(0, self.basicInfoView.bottom, kScreenW, kWDTaskDetailSectionViewHeight);
        _carSectionView = [[WDTaskDetailSectionView alloc] initWithFrame:frame];
        [self.rootContent addSubview:_carSectionView];
        [self.carSectionView updateWithViewData:@"车辆信息"];
    }
}

/// 车辆信息
- (void)initWithCarInfoView {
    if (!_carInfoView) {
        float height = kWDTaskDetailBasicSingleViewHeight;
        float batteryHeight = 0;
        if (self.viewModel.taskDetailModel.batteryList.count > 0) {
            batteryHeight = self.viewModel.taskDetailModel.batteryList.count * 2 * kWDTaskDetailBasicSingleViewHeight;
        }
        float canFaulHeight = kWDTaskDetailBasicSingleViewHeight + 14;
        if (self.viewModel.taskDetailModel.canFaultInfoList.count > 0) {
            canFaulHeight = self.viewModel.taskDetailModel.canFaultInfoList.count * kWDTaskDetailBasicSingleViewHeight + 14;
        }
        height = height + batteryHeight + canFaulHeight;
        CGRect frame = CGRectMake(0, self.carSectionView.bottom, kScreenW, height);
        _carInfoView = [[WDWorderOrdRevVehDetailCarInfoView alloc] initWithFrame:frame];
        [self.rootContent addSubview:_carInfoView];
    }
    if (self.viewModel.taskDetailModel) {
        [self.carInfoView updateWithViewData:self.viewModel.taskDetailModel];
    }
}

/// 服务流程
- (void)initWithServiceSectionView {
    if (!_serviceSectionView) {
        CGRect frame = CGRectMake(0, self.carInfoView.bottom, kScreenW, kWDTaskDetailSectionViewHeight);
        _serviceSectionView = [[WDTaskDetailSectionView alloc] initWithFrame:frame];
        [self.rootContent addSubview:_serviceSectionView];
        [self.serviceSectionView updateWithViewData:@"服务流程"];
    }
}

/// 服务流程
- (void)initWithServiceInfoView {
    if (!_serviceInfoView) {
        float height = kWDTaskDetailBasicSingleViewHeight * 5 + 14;
        WDTaskFlowModel *model = self.viewModel.taskDetailModel.taskFlowList.firstObject;
        if (model) {
            height = height - 18 + model.scanAddressHeight;
        }
        CGRect frame = CGRectMake(0, self.serviceSectionView.bottom, kScreenW, height);
        _serviceInfoView = [[WDWorderOrdRevVehDetailServiceInfoView alloc] initWithFrame:frame];
        [self.rootContent addSubview:_serviceInfoView];
    }
    if (self.viewModel.taskDetailModel) {
        [self.serviceInfoView updateWithViewData:self.viewModel.taskDetailModel];
    }
}


- (void)fromServerRequestData {
    
    @weakify(self);
    [self.viewModel requestWithTaskInfoDetail:^(EDSRespondDataEntity * _Nonnull respond) {
        @strongify(self);
        if (respond.code == EDS_NETWORK_SUCCESS) {
            [self updateWithUI];
        } else {
            [SVProgressHUD showInfoWithStatus:@"获取详情失败，请稍后再试！"];
        }
    }];
}


@end
